package com.flight.trans.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.flight.trans.R;
import com.flight.trans.base.BaseActivity;
import com.flight.trans.bean.DamageLossBean.DamageLossData;
import com.flight.trans.bean.EnterPortOrderBean.EnterPortOrderData;
import com.flight.trans.global.Constant;
import com.flight.trans.http.NetworkUtils;
import com.flight.trans.http.network.OkGoBackListener;
import com.flight.trans.util.CommonUtils;
import com.flight.trans.util.ToastUtils;
import com.flight.trans.view.dialog.AlertDialog;
import com.flight.trans.view.edit.EditorActivity;


/**
 * author : King
 * date   : 2022/4/1114:31
 * desc   : 申请事故签证
 */
public class ApplicationReceiptActivity extends BaseActivity {

    private LinearLayout mLlContainer ;
    private ImageButton mIbBack ;//返回
    private Button mBtnSubmit ;//提交

    private TextView mTvTitleName ,//标题
            mTvOrderNo ,//运单号
            mTvSender,//托运人
            mTvSenderAddress ,//托运地址
            mTvReceiver ,//发货人
            mTvReceiverAddress ,//发货地址
            mTvProductName ,//品名
            mTvPacking ,//包装
            mTvOrderCount ,//运单件数
            mTvOrderWeight ;//运单重量

    private TextView mTvAccidentCount ,//受损件数
            mTvAccidentWeight ,//受损重量
            mTvDeclaredValue ,//声明价值
            mTvInsuranceValue ,//保险价值
            mTvInspectionCondition ,//验视情况
            mTvTreatment ;//处理情况

    private CheckBox mCbLose ,//丢失
            mCbSpoil ,//损坏
            mCbLack ,//短少
            mCbDeterioration,//变质
            mCbContaminated ,//污染
            mCbOther ;//其他

    private int mOrderID , mDamageLossID;

    private EnterPortOrderData mEnterPortOrderData ;
    private DamageLossData mDamageLossData ;

    @Override
    protected int setContentView() {
        return R.layout.activity_application_receipt;
    }

    @Override
    protected void initView() {
        mLlContainer = findViewById(R.id.mLlContainer);
        mIbBack = findViewById(R.id.mIbBack);
        mBtnSubmit = findViewById(R.id.mBtnSubmit);

        mTvTitleName = findViewById(R.id.mTvTitleName);
        mTvOrderNo = findViewById(R.id.mTvOrderNo);//运单号
        mTvSender = findViewById(R.id.mTvSender);//托运人
        mTvSenderAddress = findViewById(R.id.mTvSenderAddress);//托运地址
        mTvReceiver = findViewById(R.id.mTvReceiver);//发货人
        mTvReceiverAddress = findViewById(R.id.mTvReceiverAddress);//发货地址

        mTvPacking = findViewById(R.id.mTvPacking);//包装
        mTvProductName = findViewById(R.id.mTvProductName);//品名
        mTvOrderCount = findViewById(R.id.mTvOrderCount);//运单件数
        mTvOrderWeight = findViewById(R.id.mTvOrderWeight);//运单重量

        mTvAccidentCount = findViewById(R.id.mTvAccidentCount);//受损件数
        mTvAccidentWeight = findViewById(R.id.mTvAccidentWeight);//受损重量
        mTvDeclaredValue = findViewById(R.id.mTvDeclaredValue);//声明价值
        mTvInsuranceValue = findViewById(R.id.mTvInsuranceValue);//保险价值
        mTvInspectionCondition = findViewById(R.id.mTvInspectionCondition);//验视情况
        mTvTreatment = findViewById(R.id.mTvTreatment);//处理情况

        mCbLose = findViewById(R.id.mCbLose);//丢失
        mCbSpoil = findViewById(R.id.mCbSpoil);//损坏
        mCbLack = findViewById(R.id.mCbLack);//短少
        mCbDeterioration = findViewById(R.id.mCbDeterioration);//变质
        mCbContaminated = findViewById(R.id.mCbContaminated);//污染
        mCbOther = findViewById(R.id.mCbOther);//其他
    }

    @Override
    protected void initData() {
        if(mBundle != null) {
            mOrderID = mBundle.getInt(Constant.ID);
            mDamageLossID = mBundle.getInt(Constant.DamageLossID);
        }

        mTvTitleName.setText(getString(R.string.button_application_receipt));
        CommonUtils.onFinishTouch(mActivity , mLlContainer);

        mIbBack.setOnClickListener(this::onClick);
        mBtnSubmit.setOnClickListener(this::onClick);
        mTvAccidentCount.setOnClickListener(this::onClick);
        mTvAccidentWeight.setOnClickListener(this::onClick);
        mTvDeclaredValue.setOnClickListener(this::onClick);
        mTvInsuranceValue.setOnClickListener(this::onClick);;
        mTvInspectionCondition.setOnClickListener(this::onClick);;
        mTvTreatment.setOnClickListener(this::onClick);
    }

    @Override
    protected void initConnect() {
        if(mDamageLossID == 0) {
            getOrderInfo();
        } else{
            getDamageLossData();
        }
    }

    /**
     * 获取运单信息
     */
    private void getOrderInfo(){
        NetworkUtils.getEnterPortOrderDetails(new OkGoBackListener(mActivity) {
            @Override
            public void onSuccess(Object body) {
                mEnterPortOrderData = (EnterPortOrderData) body;

                initLocationData();
            }
        }, mOrderID);
    }

    /**
     * 获取事故签证信息
     */
    private void getDamageLossData() {
        NetworkUtils.getDamageLoss(new OkGoBackListener(mActivity) {
            @Override
            public void onSuccess(Object body) {
                mDamageLossData = (DamageLossData) body;

                initLocationLossData();
            }
        }, mDamageLossID);
    }

    /**
     * 运单信息
     */
    private void initLocationData() {
        mTvOrderNo.setText(mEnterPortOrderData.getOrderNo());//运单号
        mTvSender.setText(mEnterPortOrderData.getSenderName());//托运人
        mTvSenderAddress.setText(mEnterPortOrderData.getSenderAddress());//托运地址
        mTvReceiver.setText(mEnterPortOrderData.getReceiverName());//发货人
        mTvReceiverAddress.setText(mEnterPortOrderData.getReceiverAddress());//发货地址
        mTvPacking.setText(mEnterPortOrderData.getPackName());//包装
        mTvProductName.setText(mEnterPortOrderData.getTradeName());//品名
        mTvOrderCount.setText(String.valueOf(mEnterPortOrderData.getNum()));//运单件数
        mTvOrderWeight.setText(String.valueOf(mEnterPortOrderData.getWeight()));//运单重量
    }


    /**
     * 事故签证信息
     */
    private void initLocationLossData() {
        mTvOrderNo.setText(mDamageLossData.getOrderNo());//运单号
        mTvSender.setText(mDamageLossData.getShipper());//托运人
        mTvSenderAddress.setText(mDamageLossData.getShipperAddress());//托运地址
        mTvReceiver.setText(mDamageLossData.getConsignee());//发货人
        mTvReceiverAddress.setText(mDamageLossData.getConsigneeAddress());//发货地址
        mTvProductName.setText(mDamageLossData.getTradeName());//品名
        mTvPacking.setText(mDamageLossData.getPackName());//包装
        mTvOrderCount.setText(String.valueOf(mDamageLossData.getNum()));//运单件数
        mTvOrderWeight.setText(String.valueOf(mDamageLossData.getWeight()));//运单重量

        mTvAccidentCount.setText(String.valueOf(mDamageLossData.getLossNum()));// 受损件数
        mTvAccidentWeight.setText(String.valueOf(mDamageLossData.getLossWeighe()));//受损重量
        mTvInspectionCondition.setText(mDamageLossData.getSpotChecking());//验视情况
        mTvTreatment.setText(mDamageLossData.getProcessCondition());//处理情况

        mTvDeclaredValue.setText(mDamageLossData.getDeclared());//声明价值
        mTvInsuranceValue.setText(mDamageLossData.getInsurance());//保险价值
    }


    public void onClick(View v) {
        switch (v.getId()){
            case R.id.mIbBack:
                // 返回
                onFinish();
                break;
            case R.id.mBtnSubmit:
                // 提交
                clickSubmit();
                break;
            case R.id.mTvAccidentCount:
                // 受损件数
                EditorActivity.openActivity(mActivity, new EditorActivity.EditorCallbackListener() {
                    @Override
                    public void onSubmit(String content) {
                        mTvAccidentCount.setText(content);
                    }
                } , mTvAccidentCount.getText().toString());
                break;
            case R.id.mTvAccidentWeight:
                // 受损重量
                EditorActivity.openActivity(mActivity, new EditorActivity.EditorCallbackListener() {
                    @Override
                    public void onSubmit(String content) {
                        mTvAccidentWeight.setText(content);
                    }
                } , mTvAccidentWeight.getText().toString());
                break;
            case R.id.mTvDeclaredValue:
                // 声明价值
                EditorActivity.openActivity(mActivity, new EditorActivity.EditorCallbackListener() {
                    @Override
                    public void onSubmit(String content) {
                        mTvDeclaredValue.setText(content);
                    }
                } , mTvDeclaredValue.getText().toString());
                break;
            case R.id.mTvInsuranceValue:
                // 保险价值
                EditorActivity.openActivity(mActivity, new EditorActivity.EditorCallbackListener() {
                    @Override
                    public void onSubmit(String content) {
                        mTvInsuranceValue.setText(content);
                    }
                } , mTvInsuranceValue.getText().toString());
                break;
            case R.id.mTvInspectionCondition:
                // 验视情况
                EditorActivity.openActivity(mActivity, new EditorActivity.EditorCallbackListener() {
                    @Override
                    public void onSubmit(String content) {
                        mTvInspectionCondition.setText(content);
                    }
                } , mTvInspectionCondition.getText().toString() , false);
                break;
            case R.id.mTvTreatment:
                // 处理情况
                EditorActivity.openActivity(mActivity, new EditorActivity.EditorCallbackListener() {
                    @Override
                    public void onSubmit(String content) {
                        mTvTreatment.setText(content);
                    }
                } , mTvTreatment.getText().toString() , false);
                break;
        }
    }

    /**
     * 提交
     */
    private void clickSubmit() {
        String AccidentCount = mTvAccidentCount.getText().toString();
        String AccidentWeight = mTvAccidentWeight.getText().toString();
        String InspectionCondition = mTvInspectionCondition.getText().toString();
        String Treatment = mTvTreatment.getText().toString();

        String DeclaredValue = mTvDeclaredValue.getText().toString();
        String InsuranceValue = mTvInsuranceValue.getText().toString();

        if(TextUtils.isEmpty(AccidentCount)){
            ToastUtils.showToast(R.string.toast_accident_count_null);
            return;
        }

        if(TextUtils.isEmpty(AccidentWeight)){
            ToastUtils.showToast(R.string.toast_accident_weight_null);
            return;
        }

        if(TextUtils.isEmpty(InspectionCondition)){
            ToastUtils.showToast(R.string.toast_inspection_condition_null);
            return;
        }

        if(TextUtils.isEmpty(Treatment)){
            ToastUtils.showToast(R.string.toast_treatment_null);
            return;
        }

        String accidentType = "";
        accidentType = TextUtils.isEmpty(accidentType) ? (mCbLose.isChecked() ? "1" : "") : (mCbLose.isChecked() ? accidentType + ",1" : accidentType);
        accidentType = TextUtils.isEmpty(accidentType) ? (mCbSpoil.isChecked() ? "2" : "") : (mCbSpoil.isChecked() ? accidentType + ",2" : accidentType);
        accidentType = TextUtils.isEmpty(accidentType) ? (mCbLack.isChecked() ? "3" : "") : (mCbLack.isChecked() ? accidentType + ",3" : accidentType);
        accidentType = TextUtils.isEmpty(accidentType) ? (mCbDeterioration.isChecked() ? "4" : "") : (mCbDeterioration.isChecked() ? accidentType + ",4" : accidentType);
        accidentType = TextUtils.isEmpty(accidentType) ? (mCbContaminated.isChecked() ? "5" : "") : (mCbContaminated.isChecked() ? accidentType + ",5" : accidentType);
        accidentType = TextUtils.isEmpty(accidentType) ? (mCbOther.isChecked() ? "6" : "") : (mCbOther.isChecked() ? accidentType + ",6" : accidentType);

        if(TextUtils.isEmpty(accidentType)){
            ToastUtils.showToast(R.string.toast_accident_type_null);
            return;
        }

        if(mDamageLossData == null)
            mDamageLossData = new DamageLossData(mEnterPortOrderData.getOrderNo());

        mDamageLossData.setShipper(mEnterPortOrderData.getSenderName());
        mDamageLossData.setShipperAddress(mEnterPortOrderData.getSenderAddress());
        mDamageLossData.setConsignee(mEnterPortOrderData.getReceiverName());
        mDamageLossData.setConsigneeAddress(mEnterPortOrderData.getReceiverAddress());
        mDamageLossData.setTradeName(mEnterPortOrderData.getTradeName());
        mDamageLossData.setPackName(mEnterPortOrderData.getPackName());
        mDamageLossData.setNum(mEnterPortOrderData.getNum());
        mDamageLossData.setWeight(mEnterPortOrderData.getWeight());
        mDamageLossData.setLossTradeName(mEnterPortOrderData.getTradeName());
        mDamageLossData.setLossNum(Integer.parseInt(AccidentCount));
        mDamageLossData.setLossWeighe(Double.parseDouble(AccidentWeight));
        mDamageLossData.setDeclared(DeclaredValue);
        mDamageLossData.setInsurance(InsuranceValue);
        mDamageLossData.setSpotChecking(InspectionCondition);
        mDamageLossData.setProcessCondition(Treatment);
        mDamageLossData.setAccidentType(accidentType);

        if(IsSaveIng){
            ToastUtils.showToast(R.string.toast_save_ing);
            return;
        }

        AlertDialog.getInstance(mActivity)
                .setContent(R.string.dialog_application_receipt_submit)
                .setConfirmButton(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        IsSaveIng = true;
                        NetworkUtils.saveDamageLoss(new OkGoBackListener(mActivity) {
                            @Override
                            public void onSuccess(Object body) {
                                IsSaveIng = false ;
                                ToastUtils.showToast(R.string.toast_submit_success);
                                mBundle.putInt(Constant.DamageLossID , mDamageLossID);
                                onFinish(mBundle);
                            }

                            @Override
                            public void onError() {
                                IsSaveIng = false ;
                            }
                        }, mDamageLossData , mDamageLossID);
                    }
                }).show();
    }
}
